/**
 * Stub-Element.js
 * Basic Element scripts 
 * 
 */
var stub = {};

stub.Document = {
	baseJS: '/Resources/Library.Jscript/',
	loadJS: function(library) {
		var libraries = [];
		if(Object.isArray(library))
			libraries = library;
		else
			libraries.push(library);
		
		libraries.each(function(lib) {
			if(this.baseJS)
				lib = this.baseJS + lib;
			try{
				// inserting via DOM fails in Safari 2.0, so brute force approach
				document.write('<script type="text/javascript" src="'+lib+'"><\/script>');
			} catch(e) {
				// for xhtml+xml served content, fall back to DOM methods
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = libraryName;
				document.getElementsByTagName('head')[0].appendChild(script);
			}			
		},this);
	  }
};

stub.Body = {
	dimensions: {}
};

stub.Viewport = {
	dimensions: [0,0]
};

stub.Object = {
	Copy: function(dupeObj) {
        var retObj = new Object;
        if (typeof dupeObj == "object") {
            if (typeof dupeObj.length != "undefined")
                var retObj = new Array;
            for (var objInd in dupeObj)
                if (dupeObj[objInd] == null)
                    retObj[objInd] = null;
                else if (typeof dupeObj[objInd] == "object")
                    retObj[objInd] = this.Copy(dupeObj[objInd]);
                else if (typeof dupeObj[objInd] == "string")
                    retObj[objInd] = dupeObj[objInd];
                else if (typeof dupeObj[objInd] == "number")
                    retObj[objInd] = dupeObj[objInd];
                else if (typeof dupeObj[objInd] == "boolean")
                    dupeObj[objInd] == 
                    true ? retObj[objInd] = true : retObj[objInd] = false;
        }
        return retObj;
	}
}
stub.Element = {
	/*
	 * Returns offset for element relative to immediate parent
	 */
	relativeToParent: function(element)
	{
		try
		{
			var elmLayout = new Element.Layout(element);
			var elmParentLayout = new Element.Layout(elmLayout.element.parentElement);
			
			var elmOffsets = new Element.Offset(elmLayout.element.offsetLeft, elmLayout.element.offsetTop);
			var parentOffset = new Element.Offset(elmParentLayout.element.offsetLeft, elmParentLayout.element.offsetTop);
			
			return elmOffsets.relativeTo(parentOffset);
		}
		catch(e)
		{
			var returnOffset = new Element.Offset(-1,-1);
			return returnOffset;
		}
	},
	registerInfo: function(element, message, obj) {
		element.insert(new Element('img', {
			src:'/Resources/Library.Icons/32x32/help.png'}));
		
		var clickFunc = function(message, obj) { 
			var m = message;
			if(message.evaluate && message.template)
				m = message.evaluate((obj) ? obj : {});
			_msoC.showMessage(m);
			
		}.bindAsEventListener(this);
		element.on('click', clickFunc.curry(message, obj));
	}
};

/*
 * Progress Object
 * Adds a progress bar item to an element container
 * 
 */

stub.Logging = {
	debug:true,
	logging_message: new Template('V#{version} #{class_name} - #{debug_message}'),
	log: function(message) {
		if(!this.debug)
			return;
		this.debug_message = message;
		console.log(this.logging_message.evaluate(this));
	}
};

stub.Utility = {
	clearTimer: function(timer) {
		if(!timer) return;
		try { clearTimeout(timer); } catch(e){ }
		timer = null;
	}
};
